/**
 * 
 */
package com.xcn.siteinfo;

import java.io.*;
import java.util.*;

import org.jsoup.*;
import org.jsoup.nodes.*;

import com.xcn.jdbc.*;

import net.sf.json.*;

/**
 * @author changmin
 *
 */
public class LottoWriter
{
	private static final String lottoUrl = "http://lotto.kaisyu.com/api?method=get&gno=%d";

	private static Calendar startDt = Calendar.getInstance ( );
	private static Calendar nowDt = Calendar.getInstance ( );
	
	protected final static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
	protected final static String MYSQL_URL = "jdbc:mysql://127.0.0.1:3306";
	protected final static String MYSQL_DATABASE = "OCC";
	protected final static String MYSQL_USERID = "root";
	protected final static String MYSQL_USERPW = "root99";
	
	/**
	 * 
	 */
	public LottoWriter( )
	{
		startDt.set ( 2002, 11, 7 );
	}
	
	
	/**
	 * Lotto Writer
	 * @param obj
	 */
	public void lottoWrite( JSONObject obj )
	{
		String delete = " DELETE FROM LOTTO WHERE GNO = ? ";
		String query = " INSERT INTO LOTTO ( GNO, GDATE, NUM0, NUM1, NUM2, NUM3, NUM4, NUM5, BNUM ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";
		QueryBuilder _qb = new QueryBuilder ( MYSQL_DRIVER, MYSQL_URL, MYSQL_DATABASE, MYSQL_USERID, MYSQL_USERPW );

		String gno = obj.getString ( "gno" );
		String gdate = obj.getString ( "gdate" );
		String num0 = obj.getString ( "num0" );
		String num1 = obj.getString ( "num1" );
		String num2 = obj.getString ( "num2" );
		String num3 = obj.getString ( "num3" );
		String num4 = obj.getString ( "num4" );
		String num5 = obj.getString ( "num5" );
		String bnum = obj.getString ( "bnum" );

		try
		{
			_qb.executeUpdate ( delete, gno );
			_qb.executeUpdate ( query, gno, gdate, num0, num1, num2, num3, num4, num5, bnum );
		}
		catch ( Exception e )
		{
			e.printStackTrace ( );
		}
		
	}


	/**
	 * Max Lotto Round
	 * @return
	 */
	public int getMaxRound( )
	{
		long between = nowDt.getTimeInMillis ( ) - startDt.getTimeInMillis ( );
		return ( int ) ( (between / 86400000) / 7 );
	}

	/**
	 * 현재까지 로또 번호 추출
	 * @return
	 * @throws IOException
	 */
	public JSONArray getAllLotto( ) throws IOException
	{
		JSONArray result = new JSONArray ( );
		int max = getMaxRound( );
		for ( int i = 0 ; i <= max ; i++ )
		{
			result.add ( getNumberOfLotto ( (i + 1) ) );
			System.out.println ( result.get ( i ) );
			
		}
		return result;
	}
	
	/**
	 * 회당 로또 번호 추출
	 * @param num
	 * @return
	 * @throws IOException
	 */
	public JSONObject getNumberOfLotto( int num ) throws IOException
	{
		Document doc = Jsoup.connect ( String.format ( lottoUrl, num ) ).get ( );
		Element el = doc.body ( );

		JSONObject obj = ( JSONObject ) JSONSerializer.toJSON ( el.text ( ) );
		JSONArray nums = ( JSONArray ) obj.get ( "nums" );
		obj.put ( "num0", nums.getString ( 0 ) );
		obj.put ( "num1", nums.getString ( 1 ) );
		obj.put ( "num2", nums.getString ( 2 ) );
		obj.put ( "num3", nums.getString ( 3 ) );
		obj.put ( "num4", nums.getString ( 4 ) );
		obj.put ( "num5", nums.getString ( 5 ) );

		System.out.println ( obj );
		return obj;
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main ( String[] args ) throws IOException
	{
		LottoWriter _lw = new LottoWriter ( );
		if ( args.length > 0 )
		{
			if ( args[0].equals ( "all" ) )
			{
				JSONArray result = _lw.getAllLotto ( );
				for ( int i = 0 ; i < result.size ( ) ; i++ )
				{
					_lw.lottoWrite ( result.getJSONObject ( i ) );
				}
			}
		}
		else
		{
			_lw.lottoWrite( _lw.getNumberOfLotto ( _lw.getMaxRound ( ) + 1 ) );
		}
		System.out.println ( "END...." );
	}
	
}
